{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Lab session 3: Global Optimization with Gaussian Processes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Gaussian Process Summer School, 13th September 2017.\n",
    "Written by Javier Gonzalez"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The goal of this lab session is to illustrate the concepts seen during the tutorial in Gaussian processes for Global optimization. We will focus on two aspects of Bayesian Optimization (BO): (1) the choice of the model (2) the choice of the acquisition function.\n",
    "\n",
    "The technical material associated to the methods used in this lab can be found in the slides of the tutorial. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Getting started"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In addition to GPy, this lab uses GPyOpt (http://sheffieldml.github.io/GPy/). Please be sure that it is correctly installed before starting. The easiest way is using pip. In Ubuntu machines you can do:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```\n",
    "pip install gpyopt\n",
    "```    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Some of the options of GPyOpt depend on other external packages: DIRECT, cma, pyDOE. Please be sure that this are installed if you want to use all the options. With everything installed, you are ready to start. \n",
    "\n",
    "Now, just as in the previous lab, specify to include plots in the notebook and to import relevant libraries."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%pylab inline  \n",
    "import GPy\n",
    "import GPyOpt\n",
    "import numpy as np\n",
    "from numpy.random import seed\n",
    "seed(12345)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Remembering the basics"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Before starting with the lab, remember that (BO) is an heuristic for global optimization of black-box functions. Let $f: {\\mathcal X} \\to R$ be a 'well behaved' continuous function defined on a compact subset ${\\mathcal X} \\subseteq R^d$. Our goal is to solve the global optimization problem of finding\n",
    "$$ x_{M} = \\arg \\min_{x \\in {\\mathcal X}} f(x). $$\n",
    "\n",
    "We assume that $f$ is a *black-box* from which only perturbed evaluations of the type $y_i = f(x_i) + \\epsilon_i$, with $\\epsilon_i \\sim\\mathcal{N}(0,\\sigma^2)$, are  available. The goal is to find $x_M$ by minimizing the number of evaluations of $f$. To do this, we need to determine two crucial bits:\n",
    "\n",
    "1. A **Gaussian process** that will capture the our beliefs on $f$. \n",
    "\n",
    "2. An **acquisition function** that based on the model will be useful to determine where to collect new evaluations of f. \n",
    "\n",
    "Remember that every time a new data point is collected the model is updated and the acquisition function optimized again. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Running example"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We start with a one-dimensional example. Consider here the Forrester function \n",
    "\n",
    "$$f(x) =(6x-2)^2 \\sin(12x-4),$$ defined on the interval $[0, 1]$. \n",
    "\n",
    "The minimum of this function is located at $x_{min}=0.78$. We assume that the evaluations of $f$ to are perturbed by zero-mean Gaussian noise with standard deviation 0.1. The Forrester function is part of the benchmark of functions of GPyOpt. To create the true function, the perturbed version and the boundaries of the problem you need to run the following cell. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "f_true = GPyOpt.objective_examples.experiments1d.forrester()             # true function object\n",
    "f_sim = GPyOpt.objective_examples.experiments1d.forrester(sd=.1)        # noisy version\n",
    "\n",
    "bounds = [{'name': 'var_1', 'type': 'continuous', 'domain': (0,1)}]  # problem constraints \n",
    "f_objective = f_sim.f                                         # objective function   "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To plot the true $f$, simply write:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "f_true.plot()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "f_objective contains the function that we are going to optimize. You can define your own objective but it should be able to map any numpy array of dimension $n\\times d$ (inputs) to a numpy array of dimension $n\\times 1$ (outputs). For instance:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "n = 8\n",
    "x = np.random.rand(n).reshape(n,1)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "f_objective(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The bounds of the problem shoould be defined as a tuple containing the upper and lower limits of the box in which the optimization will be performed. In our example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "bounds"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To use BO to solve this problem, we need to create a GPyOpt object in which we need to specify the following elements: \n",
    "* The function to optimize.\n",
    "* The box constraints of the problem.\n",
    "* The model, by default we use a GP with a Mattern32 kernel.\n",
    "* The acquisition function (and its parameters)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And now we have all the elements to start optimizing $f$. We create the optimization problem instance. Note that you don't need to specify the evaluation budget of. This is because at this stage we are not running the optimization, we are just initializing the different elements of the BO algorithm. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Creation of the object that we will use to run BO.\n",
    "seed(123)\n",
    "myBopt = GPyOpt.methods.BayesianOptimization(f = f_objective,        # function to optimize       \n",
    "                                             domain = bounds,        # box-constraints of the problem\n",
    "                                             acquisition_type ='EI') # acquisition = Expected improvement"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "GPyOpt.methods.BayesianOptimization?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "At this point you can access to a number of elements in myBopt, including the GP model and the current dataset (initialized at 5 random locations by default)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "myBopt.X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "myBopt.Y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "myBopt.model.model"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In case of having a problem where the evaluations of $f$ are exact you only need to include 'exact_feval=True' when creating the BO object as above. Now, to run the optimization for certain number of iterations you only need to write:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Run the optimization (may take a few senconds)\n",
    "max_iter = 5                 # evaluation budget\n",
    "myBopt.run_optimization(max_iter)   # run optimization "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And that's it! You should have receive a message describing if the method converged (two equal x's are selected in consecutive steps of the optimization) or if the maximum number of iterations was reached. In one dimensional examples, you can visualize the model and the acquisition function (normalized between 0 and 1) as follows."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "myBopt.plot_acquisition()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can only make the previous plot if the dimension of the problem is 1 or 2. However, you can always how the optimization evolved by running:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "myBopt.plot_convergence()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The first plot shows the distance between the last two collected observations at each iteration. This plot is useful to evaluate the convergence of the method. The second plot shows the best found value at each iteration. It is useful to compare different methods. The fastest the curve decreases the best is the method. See how the method converged after 10 iterations. You can also print the updated GP"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "myBopt.model.model"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Use Bayesian optimization to find the minimum of the function $f(x)= x^2 + 10 \\sin(x)$ in the interval [-10, 10]. \n",
    "\n",
    "1.1 Define the bounds of the problem, the function and check that it admits a numpy array of observations as input."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Answer to 1.1 here:\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.2 Create a GPyOpt object for global optimization using a Mattern52 kernel and adding a jitter of $0.01$ to the expected improvement acquisition (Hint: when creating the object use the option jitter = 0.01). Also remember to use 'exact_feval=True' to set the to zero the Gaussian noise of the model. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Answer to 1.2 here:\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.3 run the optimization for 10 iterations. Make and comment the convergence plots. Has the method converged? "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Answer to 1.3 here:\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Acquisition functions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this section we are going to have a look to different acquisition functions. In GPyOpt you can use the expected improvement ('EI') the maximum probability of improvement ('MPI') and the lower confidence bound. When using GPyOpt you can simply specify the acquisition that you want at the moment of creating the BO object. However, you can also load these acquisitions as separate objects."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from GPyOpt.acquisitions import AcquisitionEI, AcquisitionLCB, AcquisitionMPI "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To access these acquisitions 'externally' we create a GP model using the objective function in Section 1 evaluated in 10 locations."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "seed(1234)\n",
    "n = 10\n",
    "X = np.random.rand(n).reshape(n,1)\n",
    "Y = f_objective(X)\n",
    "m = GPy.models.GPRegression(X,Y)\n",
    "m.optimize_restarts(10)\n",
    "m.plot([0,1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we create thee objects, one for each acquisition. The jitter parameter to balance exploration and exploitation the model and the acquisition optimizers need to be specified. To work with the acquisition separately we need to create instances of these objects separately."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# GPyOpt model\n",
    "model = GPyOpt.models.GPModel(optimize_restarts=5,verbose=False)\n",
    "model.model = m\n",
    "\n",
    "# GPyOpt design space\n",
    "space = GPyOpt.Design_space(bounds)\n",
    "\n",
    "# GPyOpt acquisition optimizer\n",
    "aquisition_optimizer = GPyOpt.optimization.AcquisitionOptimizer(space)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we are ready to create the acquisitions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "acq_EI = AcquisitionEI(model,space,aquisition_optimizer,jitter=0)\n",
    "acq_LCB = AcquisitionLCB(model,space,aquisition_optimizer,exploration_weight=2)\n",
    "acq_MPI = AcquisitionMPI(model,space,aquisition_optimizer,jitter=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The objects acq_EI, acq_LCB, acq_MPI contain the acquisition functions and their gradients. By running the following piece of code you can visualize the three acquisitions. Note that we add a negative sign before each acquisition. This is because within GPyOpt these functions are minimized (instead of maximized) using gradient optimizers (like BFGS) to select new locations. In this plot, however, the larger is the value of the acquisition, the better is the point. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Plot the three acquisition functions (factor 0.1 added in in the LCB for visualization)\n",
    "X_grid = np.linspace(0,1,200)[:, None]\n",
    "plt.figure(figsize=(10,6))\n",
    "plt.title('Acquisition functions',size=25)\n",
    "plt.plot(X_grid, - 0.1*acq_LCB.acquisition_function(X_grid),label='LCB')\n",
    "plt.plot(X_grid, -acq_EI.acquisition_function(X_grid),label='EI')\n",
    "plt.plot(X_grid, -acq_MPI.acquisition_function(X_grid),label='MPI')\n",
    "plt.xlabel('x',size=15)\n",
    "plt.ylabel('a(x)',size=15)\n",
    "legend()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.1 According to the previous plot, what areas in the domain are worth expoloring and why? How can we interpret the previous plot in terms of the exploration/exploitation trade off of each one of the three acquisitions?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Answer to 2.1 here:\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.2 Now make a plot comparing the shape of the LCB acquisition (of GP-UCB in the literature) with values different values of parameters. Use the values $[0,0.1,0.25,0.5,1,2,5]$. How does the decision about where to collect the sample change when we increase the value of the parameter?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Answer to 2.2 here:\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Consider the sixhumpcamel function defined as\n",
    "$$f(x_1,x_2) = \\left(4-2.1x_1^2 + \\frac{x_1^4}{3} \\right)x_1^2 + x_1x_2 + (-4 +4x_2^2)x_2^2,$$\n",
    "\n",
    "in $[-2,2]\\times [-1,1]$. This function has two global minima, at $(0.0898,-0.7126)$ and $(-0.0898,0.7126)$. This function is also implemented in GPyOpt so, to load and visualize it simply run."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "GPyOpt.objective_examples.experiments2d.sixhumpcamel().plot()\n",
    "f_shc = GPyOpt.objective_examples.experiments2d.sixhumpcamel(sd = 0.1)  # simulated version with some noise\n",
    "\n",
    "f_shc_objective = f_shc.f\n",
    "bounds_shc =[{'name': 'var_1', 'type': 'continuous', 'domain': f_shc.bounds[0]},\n",
    "         {'name': 'var_2', 'type': 'continuous', 'domain': f_shc.bounds[1]}]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.1 Create three objects to optimize this function using the the 'EI' (with parameter equal to zero), the LCB (with parameter equal to 2) and the MPI (with parameter equal to zero). Use the same initial data in the three cases (Hint: use the options 'X' and 'Y' when creating the BO object)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Answer to 3.1\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.2 In the three cases run the optimization for 30 iterations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Answer to 3.2 here:\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.3 Now make a plot comparing the three methods. The x axis should contain the number of iterations and y axis the best found value (Hint: use .Y_best to extract from the BO objects the best current value at each iteration). Which acquisition is has the best performance in this example?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Answer to 3.3 here:\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.4 Compare the models and the acquisition functions in the three cases (after the 30 iterations). What do you observe? "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Answer to 3.4 here:\n"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
